package tongwentongshu.com.app.activity;

import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import tongwentongshu.com.app.App;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.bean.AppItem;
import tongwentongshu.com.app.bean.CancelBean;
import tongwentongshu.com.app.bean.ReadingEvent;
import tongwentongshu.com.app.contract.CallBackColonyContract;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.dialog.AlertUtils;
import tongwentongshu.com.app.network.ResponseSubscriber;
import tongwentongshu.com.app.update.UpdateFractory;
import tongwentongshu.com.app.utils.ToastUtil;

/**
 * Created by XiongBin on 2017/7/28.
 */

public class CancelReservationActivity extends BaseActivity {
    @BindView(R.id.tv_reason)
    TextView reasonTv;
    @BindView(R.id.et_reason)
    EditText reasonEt;
    private Map<String, String> map;
    int currentindex = 0;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_cancel_reservation;
    }

    @Override
    protected int getTitleId() {
        return R.string.cancel_reservation;
    }

    @Override
    protected void initView() {
        App.getApplication().addReadingActivity(this);
        bookid = getIntent().getExtras().getString("bookid");
        logid = getIntent().getExtras().getString("logid");
    }

    @Override
    protected void initData() {
        initCancelType();
    }

    @OnClick({R.id.btn_ok, R.id.ll_reason})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_ok:
                cancel();
                break;
            case R.id.ll_reason:
                if (list != null)
                    AlertUtils.initWheelDialog(mContext, list, currentindex, new CallBackColonyContract.OnWheelBack() {
                        @Override
                        public void wheelCallBack(int index) {
                            currentindex = index;
                            reasonTv.setText(list[index]);
                            AlertUtils.dismiss();
                        }
                    });
                break;
        }
    }

    String[] list;
    CancelBean msg;

    public void initCancelType() {
        map = new ArrayMap<>();
        UpdateFractory.getBuild()
                .name("CancelTypelistCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<CancelBean>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(CancelBean myshare, String msgs) {
                        msg = myshare;
                        list = new String[myshare.getData().size()];
                        for (int i = 0; i < myshare.getData().size(); i++) {
                            list[i] = myshare.getData().get(i).getName();
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });
    }

    private String bookid;
    private String logid;

    public void cancel() {
        String content = reasonEt.getText().toString().trim();
        map = new ArrayMap<>();
        map.put("bookid", bookid);
        map.put("token", Cache.getToken());
        map.put("status", "4");
        map.put("status", "4");
        map.put("typeid", msg.getData().get(currentindex).getId());
        if (!TextUtils.isEmpty(content))
            map.put("content", content);
        else
            map.put("content", "");
        map.put("logid", logid);
        Log.e(TAG, map.toString());
        UpdateFractory.getBuild()
                .name("OperationBookCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<AppItem>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(AppItem item, String msg) {
                        ToastUtil.show(mContext, msg);
//                        EventBus.getDefault().post(new ReadingEvent());
                        startActivity(MyReadingActivity.class);
                        App.getApplication().exitRead();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });

    }
}
